using System.IO;
using System.Text;
using Engine;
using Engine.Graphics;
using Engine.Media;
using Engine.Serialization;

namespace SCPAK
{
    public static class FontHandler
    {
        public static void WriteFont(Stream mainStream, Stream lstStream, Stream bitmapStream)
        {
            BinaryWriter binaryWriter = new BinaryWriter(mainStream);
            StreamReader streamReader = new StreamReader(lstStream, Encoding.UTF8);
            int num = int.Parse(streamReader.ReadLine());
            binaryWriter.Write(num);
            for (int i = 0; i < num; i++)
            {
                string[] array = streamReader.ReadLine().Split('\t');
                binaryWriter.Write(char.Parse(array[0]));
                binaryWriter.Write(float.Parse(array[1]));
                binaryWriter.Write(float.Parse(array[2]));
                binaryWriter.Write(float.Parse(array[3]));
                binaryWriter.Write(float.Parse(array[4]));
                binaryWriter.Write(float.Parse(array[5]));
                binaryWriter.Write(float.Parse(array[6]));
                binaryWriter.Write(float.Parse(array[7]));
            }
            binaryWriter.Write(float.Parse(streamReader.ReadLine()));
            string[] array2 = streamReader.ReadLine().Split('\t');
            binaryWriter.Write(float.Parse(array2[0]));
            binaryWriter.Write(float.Parse(array2[1]));
            binaryWriter.Write(float.Parse(streamReader.ReadLine()));
            binaryWriter.Write(char.Parse(streamReader.ReadLine()));
            Texture2DHandler.WriteTexture2D(mainStream, bitmapStream);
        }

        public static void Write23(Stream mainStream, Stream lstStream, Stream bitmapStream)
        {
            EngineBinaryWriter binaryWriter = new EngineBinaryWriter(mainStream);
            StreamReader streamReader = new StreamReader(lstStream, Encoding.UTF8);
            int num = int.Parse(streamReader.ReadLine());
            binaryWriter.Write(num);
            for (int i = 0; i < num; i++)
            {
                string[] array = streamReader.ReadLine().Split('\t');
                binaryWriter.Write(char.Parse(array[0]));
                binaryWriter.Write(new Vector2(float.Parse(array[1]), float.Parse(array[2])));
                binaryWriter.Write(new Vector2(float.Parse(array[3]), float.Parse(array[4])));
                binaryWriter.Write(new Vector2(float.Parse(array[5]), float.Parse(array[6])));
                binaryWriter.Write(new Vector2(float.Parse(array[7])));
            }
            binaryWriter.Write(float.Parse(streamReader.ReadLine()));
            binaryWriter.Write(new Vector2(float.Parse(streamReader.ReadLine()), float.Parse(streamReader.ReadLine())));
            binaryWriter.Write(float.Parse(streamReader.ReadLine()));
            binaryWriter.Write(char.Parse(streamReader.ReadLine()));
            int num2 = int.Parse(streamReader.ReadLine());
            binaryWriter.Write(num2);
            for (int i = 0; i < num2; i++)
            {
                string f = streamReader.ReadLine();
                if (f != null)
                {
                    binaryWriter.Write7BitEncodedInt(0);
                    binaryWriter.Write7BitEncodedInt(0);
                    binaryWriter.Write7BitEncodedInt(0);

                }
                else {
                    string[] array = f.Split('\t');
                    if (array.Length == 1)
                    {
                        binaryWriter.Write7BitEncodedInt(int.Parse(array[0]));
                        binaryWriter.Write7BitEncodedInt(0);
                        binaryWriter.Write7BitEncodedInt(0);
                    }
                    else if (array.Length == 2)
                    {
                        binaryWriter.Write7BitEncodedInt(int.Parse(array[0]));
                        binaryWriter.Write7BitEncodedInt(int.Parse(array[1]));
                        binaryWriter.Write7BitEncodedInt(0);

                    }
                    else
                    {
                        binaryWriter.Write7BitEncodedInt(int.Parse(array[0]));
                        binaryWriter.Write7BitEncodedInt(int.Parse(array[1]));
                        binaryWriter.Write7BitEncodedInt(int.Parse(array[2]));
                    }
                }
            }
            Image bitmap = Image.Load(bitmapStream, ImageFileFormat.Png);
            binaryWriter.Write(false);
            binaryWriter.Write(bitmap.Width);
            binaryWriter.Write(bitmap.Height);
            binaryWriter.Write(1);
            for (int i = 0; i < bitmap.Pixels.Length; i++)
            {
                var pix = bitmap.Pixels[i];
                binaryWriter.Write(pix.R);
                binaryWriter.Write(pix.G);
                binaryWriter.Write(pix.B);
                binaryWriter.Write(pix.A);
            }
        }

        public static void Save23(Stream lstStream, Stream textureStream,Stream sourceFileStream)
        {
            EngineBinaryReader engineBinaryReader = new EngineBinaryReader(sourceFileStream);
            int num = engineBinaryReader.ReadInt32();
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.AppendLine(num.ToString());
            for (int i = 0; i < num; i++)
            {
                char code = engineBinaryReader.ReadChar();
                Vector2 texCoord = engineBinaryReader.ReadVector2();
                Vector2 texCoord2 = engineBinaryReader.ReadVector2();
                Vector2 offset = engineBinaryReader.ReadVector2();
                float width = engineBinaryReader.ReadSingle();
                stringBuilder.AppendLine(string.Format("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\t{7}", code, texCoord.X, texCoord.Y, texCoord2.X, texCoord2.Y, offset.X, offset.Y, width));
            }
            float glyphHeight = engineBinaryReader.ReadSingle();
            Vector2 spacing = engineBinaryReader.ReadVector2();
            float scale = engineBinaryReader.ReadSingle();
            char fallbackCode = engineBinaryReader.ReadChar();
            int num2 = engineBinaryReader.ReadInt32();
            stringBuilder.AppendLine(glyphHeight.ToString());
            stringBuilder.AppendLine(spacing.X.ToString());
            stringBuilder.AppendLine(spacing.Y.ToString());
            stringBuilder.AppendLine(scale.ToString());
            stringBuilder.AppendLine(fallbackCode.ToString());
            stringBuilder.AppendLine(num2.ToString());
            for (int j = 0; j < num2; j++)
            {
                int code2 = engineBinaryReader.Read7BitEncodedInt();
                int followingCode = engineBinaryReader.Read7BitEncodedInt();
                int num3 = engineBinaryReader.Read7BitEncodedInt();
                stringBuilder.AppendLine(code2 + "\t" + followingCode + "\t" + num3);
            }
            new StreamWriter(lstStream, Encoding.UTF8).Write(stringBuilder.ToString());
            bool keepSourceImageInTag = engineBinaryReader.ReadBoolean();
            int width2 = engineBinaryReader.ReadInt32();
            int height = engineBinaryReader.ReadInt32();
            int mipLevelsCount = engineBinaryReader.ReadInt32();
            int numa = width2;
            int num2a = height;
            byte[] array = new byte[4 * numa * num2a];
            for (int i = 0; i < mipLevelsCount; i++)
            {
                engineBinaryReader.Read(array, 0, 4 * numa * num2a);
                if (i == 0)
                {
                    Image image = new Image(numa, num2a);
                    for (int j = 0; j < image.Pixels.Length; j++)
                    {
                        byte r = array[4 * j];
                        byte g = array[4 * j + 1];
                        byte b = array[4 * j + 2];
                        byte a = array[4 * j + 3];
                        image.Pixels[j] = new Color(r, g, b, a);
                    }
                    Image.Save(image, textureStream, ImageFileFormat.Png, true);
                }
                numa = MathUtils.Max(numa / 2, 1);
                num2 = MathUtils.Max(num2a / 2, 1);
            }
        }

        public static void RecoverFont(Stream lstFileStream, Stream bitmapFileStream, Stream fontStream)
        {
            BinaryReader binaryReader = new BinaryReader(fontStream);
            int num = binaryReader.ReadInt32();
            string str = "";
            str = str + num.ToString() + "\n";
            for (int i = 0; i < num; i++)
            {
                str = str + binaryReader.ReadChar().ToString() + "\t";
                str = str + binaryReader.ReadSingle().ToString() + "\t";
                str = str + binaryReader.ReadSingle().ToString() + "\t";
                str = str + binaryReader.ReadSingle().ToString() + "\t";
                str = str + binaryReader.ReadSingle().ToString() + "\t";
                str = str + binaryReader.ReadSingle().ToString() + "\t";
                str = str + binaryReader.ReadSingle().ToString() + "\t";
                str = str + binaryReader.ReadSingle().ToString() + "\n";
            }
            str = str + binaryReader.ReadSingle().ToString() + "\n";
            str = str + binaryReader.ReadSingle().ToString() + "\t" + binaryReader.ReadSingle().ToString() + "\n";
            str = str + binaryReader.ReadSingle().ToString() + "\n";
            str += binaryReader.ReadChar().ToString();
            lstFileStream.Write(Encoding.UTF8.GetBytes(str), 0, Encoding.UTF8.GetBytes(str).Length);
            Texture2DHandler.RecoverTexture2D(bitmapFileStream, fontStream);
        }
    }
}
